package com.iflyun.redis.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;

import com.iflyun.redis.dao.InitRedisDao;
import com.iflyun.redis.entity.Bus;

public class InitRedisDaoImpl implements InitRedisDao {

	@Resource(name="dataSource")
	private DataSource dataSource;
	
	@Override
	public List<Bus> getList(String line,String type) {
		JdbcTemplate jt= new JdbcTemplate(dataSource);
		List<Bus> list =new ArrayList<>();
		String sql="";
		if(StringUtils.isEmpty(line)){
			sql="select s.station_id as id,l.line_no as no,s.station_name as name,substr(s.station_name,1,instr(s.station_name,'-',-1)-1) "
					+ " as realName from biz_bus_line l join biz_bus_line_station d on d.line_no = l.line_no "
					+ " join biz_bus_station s on s.station_id = d.station_id join biz_bus_station_temp sb"
					+ " on sb.station_name=substr(s.station_name, 1, instr(s.station_name, '-', -1) - 1) "
					+ " where d.line_direct = ? order by d.station_no asc";
			list = jt.query(sql, new BeanPropertyRowMapper<Bus>(Bus.class), type);
		}else{
			sql="select s.station_id as id,l.line_no as no,s.station_name as name,substr(s.station_name,1,instr(s.station_name,'-',-1)-1) "
					+ " as realName from biz_bus_line l join biz_bus_line_station d on d.line_no = l.line_no "
					+ " join biz_bus_station s on s.station_id = d.station_id join biz_bus_station_temp sb"
					+ " on sb.station_name=substr(s.station_name, 1, instr(s.station_name, '-', -1) - 1) "
					+ " where d.line_direct = ? and l.line_no= ?  order by d.station_no asc";
			list = jt.query(sql, new BeanPropertyRowMapper<Bus>(Bus.class), type,line);
		}

		return list;
	}

}
